home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / Color Picker / Color Picker.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  7.3 KB  |  299 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Color Picker.c
  3.  
  4.     Contains:    This application demonstrates how to use the color        
  5.                 picker package to dynamically change colors in a        
  6.                 custom palette.  The program basically displays 16        
  7.                 squares, each representing a separate entry in the        
  8.                 palette.  Clicking the mouse in any box allows the        
  9.                 user to redefine that box's color with the color        
  10.                 picker package routines.  The trap ActivatePalette        
  11.                 is called after each color change to guarantee the        
  12.                 window is updated with the palette changes.  Without
  13.                 this safeguard, random results may occur and the        
  14.                 color change may not take affect until the window        
  15.                 has physically changed or moved.  Finally, the first    
  16.                 and last entries in the palette cannot be changed        
  17.                 and are always defined to black and white.  This is        
  18.                 done because ActivatePalette will only update 14        
  19.                 non-b/w Tolerant colors.  The remaining 2 colors        
  20.                 if defined as b/w will be updated, otherwise QD will    
  21.                 return the color in the palette which is the closest    
  22.                 match to the RGB values of these entries.                
  23.  
  24.     Written by: EL    
  25.  
  26.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  27.  
  28.                 You may incorporate this Apple sample source code into your program(s) without
  29.                 restriction. This Apple sample source code has been provided "AS IS" and the
  30.                 responsibility for its operation is yours. You are not permitted to redistribute
  31.                 this Apple sample source code as "Apple sample source code" after having made
  32.                 changes. If you're going to re-distribute the source, we require that you make
  33.                 it clear in the source that the code was descended from Apple sample source
  34.                 code, but that you've made changes.
  35.  
  36.     Change History (most recent first):
  37.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  38.                                             for demonstration purposes.
  39.                 7/8/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  40.                 
  41.  
  42. */
  43.  
  44. #include "CarbonPrefix.h"
  45. #include <AppleEvents.h>
  46. #include <Errors.h>
  47. #include <Events.h>
  48. #include <Fonts.h>
  49. //#include <GestaltEqu.h>
  50. #include <Gestalt.h>
  51. #include <Memory.h>
  52. #include <Menus.h>
  53. #include <OSUtils.h>
  54. #include <QDOffscreen.h>
  55. #include <QuickDraw.h>
  56. #include <Resources.h>
  57. #include <Script.h>
  58. #include <ToolUtils.h>
  59. #include <Windows.h>
  60. #include <Palettes.h>
  61. #include <TextEdit.h>
  62. #include <Dialogs.h>
  63. #include <ColorPicker.h>
  64. #include <Sound.h>
  65.  
  66. /* Constant Declarations */
  67.  
  68. #define    TOTALCOLORS    16
  69.  
  70. #define    BOXSIZE        75
  71. #define    WWIDTH        ((TOTALCOLORS / 4) * BOXSIZE)
  72. #define    WHEIGHT        ((TOTALCOLORS / 4) * BOXSIZE )
  73.  
  74. //#define WLEFT        (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  75. //#define WTOP        (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  76.  
  77. /* Global Variable Definitions */
  78.  
  79. WindowPtr            gWindow;
  80. PaletteHandle        gPalette;
  81.  
  82. void initMac();
  83. void createWindow();
  84. void createPalette();
  85. void drawImage();
  86.  
  87. void doEventLoop();
  88. void doInContent();
  89.  
  90.  
  91. void main(void)
  92. {
  93.     initMac();
  94.     
  95.     createWindow();
  96.     createPalette();
  97.     drawImage();
  98.  
  99.     doEventLoop();
  100.  
  101.     DisposeWindow( gWindow );
  102. }
  103.  
  104.  
  105.  
  106. void initMac()
  107. {
  108.     //MaxApplZone();
  109.  
  110.     //InitGraf( &qd.thePort );
  111.     //InitFonts();
  112.     //InitWindows();
  113.     //InitMenus();
  114.     //TEInit();
  115.     //InitDialogs( nil );
  116.     InitCursor();
  117.     FlushEvents( 0, everyEvent );
  118. }
  119.  
  120. void createWindow()
  121. {
  122.     Rect wBounds;
  123.     int left, top;
  124.     BitMap    tempBitMap;
  125.     
  126.     GetQDGlobalsScreenBits(&tempBitMap);
  127.     
  128.     left = (((tempBitMap.bounds.right - tempBitMap.bounds.left) - WWIDTH) / 2);
  129.     top = (((tempBitMap.bounds.bottom - tempBitMap.bounds.top) - WWIDTH) / 2);
  130.     
  131.     /* Create a window to display the image. */
  132.     
  133.     //SetRect( &wBounds, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  134.     SetRect( &wBounds, left, top, left + WWIDTH, top + WHEIGHT );
  135.     
  136.     gWindow = NewCWindow( 0L, &wBounds, "\pColor Picker Test", true, documentProc,
  137.                             (WindowPtr)-1L, true, 0L );
  138.                             
  139.     ShowWindow( gWindow );
  140.     //SetPort( gWindow );
  141.     SetPortWindowPort( gWindow );
  142. }
  143.  
  144. void createPalette()
  145. {
  146.     int    index;
  147.     RGBColor aColor;
  148.  
  149.     /* Create the palette of size TOTALCOLORS. */
  150.     
  151.     gPalette = NewPalette( TOTALCOLORS, nil, pmTolerant, 0 );
  152.     
  153.     /* Assign a color to the first 15 palette entries. */
  154.  
  155.     for (index = 0; index < (TOTALCOLORS - 1); index++)
  156.     {
  157.         aColor.blue = index * (0xffff / (TOTALCOLORS - 2));
  158.         aColor.red = aColor.green = 0;
  159.         SetEntryColor( gPalette, index, &aColor );
  160.     }
  161.     
  162.     /* Set the last entry to white. */
  163.     
  164.     aColor.red = aColor.green = aColor.blue = 0xffff;
  165.     SetEntryColor( gPalette, (TOTALCOLORS - 1), &aColor );
  166.  
  167.     /* Attach the new palette to the main window. */
  168.     
  169.     SetPalette( gWindow, gPalette, true );
  170. }
  171.  
  172. void drawImage()
  173. {
  174.     int            i;
  175.     int            x, y;
  176.     Rect        rect;
  177.     RGBColor    aColor;
  178.  
  179.     /* Draw a grid of colors to represent each color entry in the palette. */
  180.  
  181.     for (i = 0; i < TOTALCOLORS; i++)
  182.     {
  183.         x = (i % 4) * BOXSIZE;
  184.         y = (i / 4) * BOXSIZE;
  185.         
  186.         GetEntryColor( gPalette, i, &aColor );
  187.         RGBForeColor( &aColor );
  188.         
  189.         SetRect( &rect, x, y, x + BOXSIZE, y + BOXSIZE );
  190.         PaintRect( &rect );
  191.     }
  192. }
  193.  
  194. void doInContent( thePoint )
  195. Point thePoint;
  196. {
  197.     int            paletteIndex = -1;
  198.     Point        where;
  199.     RGBColor    currentColor, newColor;
  200.     Rect        rect;
  201.  
  202.     /* Get the palette entry index for the box drawn at the mouse click. */
  203.     
  204.     paletteIndex = ((thePoint.v / BOXSIZE) * 4) + (thePoint.h / BOXSIZE);
  205.     
  206.     /* If the paletteIndex isn't the first or last entry, do the following. */
  207.  
  208.     if (paletteIndex > 0 && paletteIndex < (TOTALCOLORS - 1))
  209.     {
  210.         /* Invert the selected box then beep the Mac. */
  211.     
  212.         SetRect( &rect, (paletteIndex % 4) * BOXSIZE,
  213.                     (paletteIndex / 4) * BOXSIZE,
  214.                     ((paletteIndex % 4) * BOXSIZE) + BOXSIZE,
  215.                     ((paletteIndex / 4) * BOXSIZE) + BOXSIZE );
  216.         InvertRect( &rect );
  217.         SysBeep( 1 );
  218.     
  219.         /* Get the RGB values for the color stored at this palette index. */
  220.         
  221.         GetEntryColor( gPalette, paletteIndex, ¤tColor );
  222.         
  223.         /* Open the color picker dialog to select new RGB values. */
  224.         
  225.         where.h = where.v = -1;
  226.  
  227.         if (GetColor( where, "\pSelect a new palette color.", ¤tColor, &newColor ))
  228.         {
  229.             /* Assign the new RGB values to this entry. */
  230.             
  231.             SetEntryColor( gPalette, paletteIndex, &newColor );
  232.             
  233.             /* Update the palette with the new colors. */
  234.             
  235.             ActivatePalette( gWindow );
  236.             
  237.             /* Redraw the image with the new palette colors. */
  238.         
  239.             drawImage();
  240.         }
  241.         else
  242.         {
  243.             /* Invert the rect back to its original state on a Cancel. */
  244.             
  245.             InvertRect( &rect );
  246.         }
  247.     }
  248. }
  249.  
  250. void doEventLoop()
  251. {
  252.     EventRecord anEvent;
  253.     WindowPtr   evtWind;
  254.     short       clickArea;
  255.     Rect        screenRect;
  256.     Point        thePoint;
  257.  
  258.     for (;;)
  259.     {
  260.         if (WaitNextEvent( everyEvent, &anEvent, 0, nil ))
  261.         {
  262.             if (anEvent.what == mouseDown)
  263.             {
  264.                 clickArea = FindWindow( anEvent.where, &evtWind );
  265.                 
  266.                 if (clickArea == inDrag)
  267.                 {
  268.                     //screenRect = (**GetGrayRgn ()).rgnBBox;
  269.                     GetRegionBounds( GetGrayRgn(), &screenRect );
  270.                     DragWindow( evtWind, anEvent.where, &screenRect );
  271.                 }
  272.                 else if (clickArea == inContent)
  273.                 {
  274.                     if (evtWind != FrontWindow())
  275.                         SelectWindow( evtWind );
  276.                     else
  277.                     {
  278.                         thePoint = anEvent.where;
  279.                         GlobalToLocal( &thePoint );
  280.                         doInContent( thePoint );
  281.                     }
  282.                 }
  283.                 else if (clickArea == inGoAway)
  284.                     if (TrackGoAway( evtWind, anEvent.where ))
  285.                         return;
  286.             }
  287.             else if (anEvent.what == updateEvt)
  288.             {
  289.                 evtWind = (WindowPtr)anEvent.message;    
  290.                 //SetPort( evtWind );
  291.                 SetPortWindowPort( evtWind );
  292.                 
  293.                 BeginUpdate( evtWind );
  294.                 drawImage();
  295.                 EndUpdate (evtWind);
  296.             }
  297.         }
  298.     }
  299. }